using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls
{
	internal sealed class OpacityAnimator
	{
		private static readonly PropertyPath OpacityPropertyPath = new PropertyPath("Opacity", new object[0]);
		private readonly Storyboard _sbRunning = new Storyboard();
		private readonly DoubleAnimation _daRunning = new DoubleAnimation();
		private bool _suppressChangeNotification;
		private Action _oneTimeAction;
		public OpacityAnimator(UIElement target)
		{
			this._sbRunning.add_Completed(new EventHandler(this.OnCompleted));
			this._sbRunning.get_Children().Add(this._daRunning);
			Storyboard.SetTarget(this._daRunning, target);
			Storyboard.SetTargetProperty(this._daRunning, OpacityAnimator.OpacityPropertyPath);
		}
		public void GoTo(double targetOpacity, Duration duration)
		{
			this.GoTo(targetOpacity, duration, null, null);
		}
		public void GoTo(double targetOpacity, Duration duration, Action completionAction)
		{
			this.GoTo(targetOpacity, duration, null, completionAction);
		}
		public void GoTo(double targetOpacity, Duration duration, IEasingFunction easingFunction, Action completionAction)
		{
			this._daRunning.set_To(new double?(targetOpacity));
			this._daRunning.set_Duration(duration);
			this._daRunning.set_EasingFunction(easingFunction);
			this._sbRunning.Begin();
			this._suppressChangeNotification = true;
			this._sbRunning.SeekAlignedToLastTick(TimeSpan.Zero);
			this._oneTimeAction = completionAction;
		}
		private void OnCompleted(object sender, EventArgs e)
		{
			Action oneTimeAction = this._oneTimeAction;
			if (oneTimeAction != null)
			{
				this._oneTimeAction = null;
				oneTimeAction.Invoke();
			}
			if (!this._suppressChangeNotification)
			{
				this._suppressChangeNotification = false;
			}
		}
		public static void EnsureAnimator(UIElement targetElement, ref OpacityAnimator animator)
		{
			if (animator == null)
			{
				animator = new OpacityAnimator(targetElement);
			}
			if (animator == null)
			{
				throw new InvalidOperationException("The animation system could not be prepared for the target element.");
			}
		}
	}
}
